#include "serializer/protobuf_serializer.h"
#include <cstring>
#include <functional>
#include <google/protobuf/message.h>
#include <google/protobuf/util/json_util.h>

std::vector<uint8_t> ProtobufSerializer::serialize(const google::protobuf::Message& message) {
    std::vector<uint8_t> result;
    
    try {
        size_t byte_size = message.ByteSizeLong();
        if (byte_size == 0) {
            return result;
        }
        
        result.resize(byte_size);
        if (!message.SerializeToArray(result.data(), byte_size)) {
            result.clear();
            return result;
        }
        
    } catch (const std::exception& e) {
        result.clear();
    }
    
    return result;
}

bool ProtobufSerializer::deserialize(const std::vector<uint8_t>& serialized_data, 
                                           google::protobuf::Message& message) {
    if (serialized_data.empty()) {
        return false;
    }

    try {
        
        return message.ParseFromArray(serialized_data.data(), serialized_data.size());
        
    } catch (const std::exception& e) {
        return false;
    }
}

